<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    function areAlmostEqual(s1, s2) {
        if (s1 == s2) {
            return true;
        }
        if (s1.length != s2.length) {

            return false;
        } else {
            //每次一改变s1的值，s的就变,所以要交换之后再交换回来
            var s = s1.split('');
            let i, j;

            for (i = 0; i < s1.length; i++) {
                for (j = i + 1; j < s1.length; j++) {

                    // console.log('*******', s)
                    // 两个字母交换--重新定义s
                    s[j] = s1[i] + '';
                    s[i] = s1[j] + '';
                    //  console.log('s[i]', s[i], '    s[j]', s[j]);
                    //  console.log('s', s);
                    let str = s.join('');
                    if (str == s2) {
                        return true;
                    } else {
                        //交换回来
                        s[i] = s1[i];
                        s[j] = s1[j];
                        continue;
                    }
                }
            }
            if (i == s1.length) {
                return false;
            }

        }
    };

    console.log(areAlmostEqual('bank', 'kanb'))
</script>

</html>